import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.plaf.LayerUI;

/**
 * 
 */

/**
 * @author Administrador
 *
 */
public class DrawJApplet extends JApplet {

	/* (non-Javadoc)
	 * @see java.applet.Applet#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}

	/* (non-Javadoc)
	 * @see java.applet.Applet#init()
	 */
	@Override
	public void init() {
		super.init();
		JPanel panel = createPanel();
		LayerUI<JComponent> layerUI = new MyLayerUI();
		JLayer<JComponent> layer = new JLayer<JComponent>(panel, layerUI);
		add(layer);
	}

	private JPanel createPanel() {
		JPanel panel = new JPanel();
		panel.add(new JPanel());
		panel.add(new JPanel());
		((JPanel)panel.getComponents()[0]).setBorder(BorderFactory.createLineBorder(Color.red));
		((JPanel)panel.getComponents()[1]).setBorder(BorderFactory.createLineBorder(Color.blue));
		return panel;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame("DrawApp");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				JApplet drawApplet = new DrawJApplet();
				frame.add(drawApplet);
				drawApplet.init();
				frame.pack();
				frame.setVisible(true);
			}
		});

	}
	class MyLayerUI extends LayerUI<JComponent>{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		/* (non-Javadoc)
		 * @see javax.swing.plaf.LayerUI#paint(java.awt.Graphics, javax.swing.JComponent)
		 */
		@Override
		public void paint(Graphics g, JComponent c) {
			super.paint(g, c);
	System.out.println("paint");
			int w = c.getWidth();
			int h = c.getHeight();
			
			Graphics2D g2 = (Graphics2D)g.create();
			
			g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
			g2.setPaint(new GradientPaint(0,0,Color.yellow,0,h,Color.green));
			g2.fillRect(0,0,w,h);
			
			g2.dispose();
		}

	}
}

